<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Error_Handler extends Controller_Initial_Page {
       
   /**
     * @var string
     */ 
    protected $_requested_page; 
 
    /**
     * @var string
     */ 
    protected $_message; 
 
    /**
     * Pre determine error display logic
     */ 
    public function before() 
    { 
        parent::before(); 
 
        $this->auto_render = true;
        // Sub requests only! 
   
        if ( ! $this->request->is_initial()) 
        { 
           
            if ($message = rawurldecode($this->request->param('message'))) 
            { 
                $this->_message = $message; 
            } 
 
            if ($requested_page = rawurldecode($this->request->param('origuri'))) 
            { 
                $this->_requested_page = $requested_page; 
            }
        } 
        else 
        {  
            
            // This one was directly requested, don't allow 
            $this->request->action('internal_404');
            
            // Set the requested page accordingly 
            $this->_requested_page = Arr::get($_SERVER, 'REQUEST_URI'); 
        }
        
        $this->response->status((int) substr($this->request->action(),9)); 
    } 

    
    
    /**
     * Serves HTTP 404 error page
     */ 
    public function action_internal_404() 
    { 
        $this->description = 'The requested page not found';
        $this->title = 'Page not found'; 
        $this->keywords = array('not found', '404');
        
        $error_view = View::factory('error/404');
        $error_view->set('error_message', $this->_message) 
                   ->set('requested_page', $this->_requested_page); 
        $content_view = View::factory('content');
        $content_view->set('content',$error_view);

        $this->template->set('body', $content_view);  
        
    } 
 
    /**
     * Serves HTTP 500 error page
     */ 
    public function action_internal_500() 
    { 
        $this->description = 'Internal server error occured'; 
        $this->keywords = 'server error, 500, internal error, error'; 
        $this->title = 'Internal server error occured'; 
 
        $this->view = View::factory('error/500'); 
    } 

}

